package com.example.shaidemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.app.Activity;
import android.content.Intent;

import android.content.SharedPreferences;
import android.graphics.Bitmap;

import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


import java.text.SimpleDateFormat;
import java.util.Date;

public class UploadTipsActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "UploadTipsActivity";

    EditText title,content;
    Spinner spinner;
    String title_text,content_text,type;
    String []titleArray;
    ImageView photo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_tips);

        //获取组件
        title = findViewById(R.id.t_title_input);
        content = findViewById(R.id.t_content_input);
        //获取类型
        spinner = findViewById(R.id.t_spinner);
        titleArray = getResources().getStringArray(R.array.titleArray);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                // 获取被选中的值
                type = titleArray[position];
                Log.i(TAG, "帖子的标题为: " + type);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {         }
        });

        //提交
        // 返回原页面
        ImageView pic = findViewById(R.id.collection_pic);
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UploadTipsActivity.this,MainActivity.class);
                intent.putExtra("fragment_flag",2);
                startActivity(intent);
            }
        });

        //提交照片
        photo = findViewById(R.id.open_cream);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //执行OpenCamera（）方法
                OpenCamera();

            }
        });
    }

    // 检查、提交填写资料，并返回页面
    @Override
    public void onClick(View view) {
        //获取用户输入
        title_text = title.getText().toString().trim();
        content_text = content.getText().toString().trim();

        //判断输入是否为空
        CheckInput ck = new CheckInput();
        boolean flag = ck.CheckInputNull(title_text) &&ck.CheckInputNull(content_text) ;
        if(flag){
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
            String time =  sdf.format(date);

            //将数据写到SP里
            SharedPreferences sharedPreferences = getSharedPreferences("newTips", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("class","防杀熟");
            editor.putString("status","未审核");
            editor.putString("time",time);
            editor.putString("title",title_text);
            editor.putString("type","类别："+type);
            editor.putString("content",content_text);
            editor.commit();
            Log.i(TAG, "onActivityResult: 数据已保存到sharedPreferences");

            //提示消息
            Toast.makeText(this,"上传成功！",Toast.LENGTH_SHORT).show();

            // 返回页面
            finish();
        }else{
            // 提示信息
            Toast.makeText(this,"输入不能为空，请再次检查输入！",Toast.LENGTH_SHORT).show();
        }
    }

    //打开拍照界面
    public void OpenCamera(){
        Intent open = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(open,2);
    }

    // 获取相机所拍到的图片，并执行SaveScreenShot()方法保存图片
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 2 && resultCode == RESULT_OK && null !=data) {
            Bundle bundle = data.getExtras();
            //获取相机返回的数据，并转换为Bitmap图片格式，即缩略图
            Bitmap bitmap = (Bitmap) bundle.get("data");
            //保存图片
            SavePicture sa = new SavePicture();
            String path = sa.SaveScreenShot(bitmap, this);
            Log.i(TAG, "onActivityResult: Uri ===" + path);
            //显示所拍摄的照片
            photo.setImageBitmap(bitmap);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

}